<script>
    layui.config({
        base: '/static/customAdmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'table', 'admin', 'upload'], async () => {
        const table = layui.table, form = layui.form, admin = layui.admin, upload = layui.upload, $ = layui.$;
        const upload_clearance = upload.render({
            elem: '#clearance_upload'
            , url: '/clearance/import/' + $("#clearance_type_select").val()
            , accept: 'file'
            , field: 'filename'
            , before: function (obj) {
                layer.load(2);
            }
            , done: function (res) {
                if (res.status) {
                    layer.msg(res.msg, {
                        offset: '15px'
                        , icon: 1
                        , time: 2000
                        , id: 'Message'
                    });
                } else {
                    layer.msg(res.msg, {
                        offset: '15px'
                        , icon: 2
                        , time: 2000
                        , id: 'Message'
                    });
                }
                table.reload('clearance_lists');
                layer.closeAll('loading');
            }
            , error: function () {
                layer.closeAll('loading');
            }
        });

        table.render({
            elem: '#clearance_lists'
            , autoSort: false
            , skin: 'line'
            , method: 'post'
            , where: {
                order: 'desc',
                sort: 'CreatedAt'
            }
            , contentType: 'application/json'
            , url: @{{ urlfor "ClearanceController.DataGrid" }}
            , request: {
                pageName: 'offset' //页码的参数名称，默认：page
                , limitName: 'limit' //每页数据量的参数名，默认：limit
            }, response: {
                statusName: 'code',
                statusCode: 0,
            }
            , parseData: function (res) {
                return {
                    "code": res.code,
                    "count": res.total,
                    "data": res.rows
                };
            }
            , toolbar: true
            , defaultToolbar: ['filter']
            , colFilterRecord: 'local'
            , cols: [[
                {field: 'CustomsCode', title: '海关编码', sort: true}
                , {field: 'InspectionCode', title: '商检编码', sort: true}
                , {field: 'Name', title: '中文名称', sort: true}
                , {field: 'ShortName', title: '中文简称', sort: true}
                , {field: 'EnName', title: '英文名称', sort: true}
                , {field: 'ShortEnName', title: '英文简称', sort: true}
                , {field: 'Remark', title: '备注', sort: true}
                , {title: '操作', toolbar: '#clearance_toolbar', width: 120}
            ]]
            , page: true
            , limit: 10
        });

        table.on('sort(clearance_lists)', function (obj) {
            table.reload('clearance_lists', {
                initSort: obj
                , where: {
                    orderBy: obj.field
                    , sortedBy: obj.type
                }
            });
        });

        $("#clearance_search").on('keydown', function (e) {
            let eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
            if (eCode === 13) {
                table.reload('clearance_lists', {
                    where: {
                        CustomsCodeLike: $(this).val()
                    },
                    page: {
                        curr: 1
                    }
                });
            }
        });

        $("#clearance_new").on('click', function () {
            layer.open({
                type: 2,
                title: '新增字段',
                content: `/clearance/create/`,
                shadeClose: true,
                area: admin.screen() < 2 ? ['100%', '80%'] : ['630px', '575px'],
                maxmin: true
            });
        });

        table.on('tool(clearance_lists)', function (obj) {
            const data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除么', {title: '提示'}, async (index) => {
                    await admin.delete(`/clearance/delete/${data.Id}`, obj);
                    layer.close(index);
                    table.reload('clearance_lists');
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '编辑字段'
                    , content: `/clearance/edit/${data.Id}`
                    , shadeClose: true
                    , area: admin.screen() < 2 ? ['100%', '80%'] : ['630px', '575px']
                    , maxmin: true
                });
            }
        });

        let lastUpdateTime = await admin.get("/clearance/last_update_time/1");
        $("#lastUpdateTime").text("最近更新：" + lastUpdateTime);

        form.on('select(type)', async function (data) {
            let lastUpdateTime = await admin.get("/clearance/last_update_time/" + data.value);
            $("#lastUpdateTime").text("最近更新：" + lastUpdateTime);
            table.reload('clearance_lists', {
                where: {
                    Type: data.value ? `${data.value}` : ''
                }
            });
        });
    });
</script>